<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

$shopId = $_GET['shopId'];
$serviceId = $_GET['serviceId'];
$startHour = $_GET['startHour'];
$startMinute = $_GET['startMinute'];
$orderId = $_GET['id'];

if( $shopId < 1 || $serviceId < 1 ){
	Output::Json("大変申し訳ございませんでした。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。", -11);
	exit;
}
if( $serviceId < 1 ){
	//$staffList = Staff::GetAllByShop($shopId);
}else{
	$staffList = Staff::GetAllByShopAndService($shopId, $serviceId);
}
$staffIdList = array();

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['start_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$getTimeArray = explode(" ", $getDateArray[2]);
$day = $getTimeArray[0] * 1;
$getTimeArray = explode(":", $getTimeArray[1]);
$startHour = $getTimeArray[0];
$startMinute = $getTimeArray[1];

$year = $year < 1 ? $tmp_year : $year;
$month = $month < 1 ? $tmp_mon : $month;
$day = $day < 1 ? $tmp_day : $day;

if($endHour < 1){
	$service = Service::GetById($serviceId);
	$endMinute = $startMinute + $service['service_duration'];
	$endHour = $startHour;
	while ($endMinute > 59){
		$endMinute = $endMinute - 60;
		$endHour = $endHour + 1;
	}
}
$startTime = mktime($startHour, $startMinute, 0, $month, $day, $year);
$endTime = mktime($endHour, $endMinute, 0, $month, $day, $year);

$now = time();
if( $now > $startTime ){
	Output::Json("大変申し訳ございませんでした。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。", -1);
	exit;
}
$tempOrder = Preengage::GetById($orderId);
foreach ($staffList as $key => $value) {

	
	$temp = WorkSchedule::HasWork($startTime, $endTime + $service['reset_device_time'] * 60, $value['id']);
	if( $temp == null ){
		Logs::setLog($value['id']."|| is not work that time<>".date("y-m-d H:i", $startTime)."<>".date("y-m-d H:i", $endTime + $service['reset_device_time'] * 60));
		continue;
	}
	
	$temp = Preengage::HasOrdered($startTime, $endTime + $service['reset_device_time'] * 60, $value['id']);
	
	if( $tempOrder != null && $tempOrder['designation_stuff'] == $value['id'] ){
		
		$staffIdList[ $value['id'] ] = $value['family_name'].' '.$value['personal_name'];
		continue;
	}
	
	if($temp != null){
		Logs::setLog($value['id']."|| has order that time<>".date("y-m-d H:i", $startTime)."<>".date("y-m-d H:i", $endTime + $service['reset_device_time'] * 60));
		continue;
	}
	
	$temp = Absence::HasAbsence($startTime, $endTime + $service['reset_device_time'] * 60, $value['id']);
	if($temp != null){
		Logs::setLog($value['id']."|| has absenced that time<>".date("y-m-d H:i", $startTime)."<>".date("y-m-d H:i", $endTime + $service['reset_device_time'] * 60));
		continue;
	}
	$staffIdList[ $value['id'] ] = $value['family_name'].' '.$value['personal_name'];
}


Output::Json($staffIdList);

